package grape.workflow.rest.comment.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import grape.common.exception.ExceptionTools;
import grape.common.rest.mvc.SuperController;
import grape.workflow.rest.comment.form.CommentListPageForm;
import grape.workflow.rest.comment.mapper.CommentWebMapper;
import grape.workflow.rest.comment.vo.CommentVo;
import grape.workflow.service.comment.api.IWorkflowCommentService;
import grape.workflow.service.comment.dto.CommentDto;
import grape.workflow.service.comment.query.CommentQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yangwei
 * Created at 2020/6/24 15:31
 */
@RestController
@RequestMapping("/workflow/comment")
@Api(tags = "评论相关接口")
public class CommentController extends SuperController {

    @Autowired
    private IWorkflowCommentService iCommentService;
    @Autowired
    private CommentWebMapper commentWebMapper;

    @ApiOperation("评论分页列表接口")
    @PreAuthorize("hasAuthority('process:comment:listPage')")
    @GetMapping(value = "/comment/ListPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<CommentVo> listPage(CommentListPageForm commentListPageForm) {

        IPage page = new Page(commentListPageForm.getCurrent(), commentListPageForm.getSize());
        CommentQuery commentParam = commentWebMapper.map(commentListPageForm);
        page = iCommentService.listPage(page, commentParam);
        if (page == null || isEmpty(page.getRecords())) {
            throw ExceptionTools.dataNotExistRE("暂无数据");
        }
        List<CommentDto> records = page.getRecords();
        List<CommentVo> collect = records.stream().map(commentWebMapper::dtoToVo).collect(Collectors.toList());
        page.setRecords(collect);

        return page;
    }
}
